<template>
  <el-container style="height: 950px; border: 1px solid #eee">
    <el-aside width="350px" >
      <el-container >
        <!--   患者选择区      -->
        <el-header style="padding: 0;margin: 0">
          <el-row>
            <el-col :span="21" style="text-align: left">
              <el-tag>患者选择：</el-tag>
            </el-col>
            <el-col :span="3" style="text-align: right">
              <el-button type="primary"  size="small" @click="reloadUser" style="margin-right: 5px">
                <el-icon><Refresh/></el-icon>
              </el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-input placeholder="请输入内容" v-model="inputPatient"  style="width: 100%">
            <!--@slot 插槽前方添加内容-->
              <template #prepend>
                患者名：
              </template>
            <!--@slot 插槽后方添加内容-->
              <template #append>
                <el-button type="primary"  size="small" @click="checkwithname" >
                  <el-icon><Search/></el-icon>
                </el-button>
              </template>
            </el-input>
          </el-row>
        </el-header>
        <el-main style="height: 800px;padding: 0;margin: 0">
          <el-tabs type="border-card"  v-model="activeTab">
            <el-tab-pane label="本人" name="personal">
              <el-tag size="small" >未诊患者：</el-tag>
              <el-table ref="singleTable" :data="RegistersTable01" highlight-current-row
                        @row-dblclick = "handleDBCurrentChange"
                        style="width: 100%;height: 50%" :show-header="false" max-height="400" size="small">
                <el-table-column property="casenumber" label="病历号" width="80">
                </el-table-column>
                <el-table-column property="realname" label="姓名" >
                </el-table-column>
                <el-table-column label="年龄" width="50">
                  <template #default="solt">
                    {{solt.row.age}}{{solt.row.agetype}}
                  </template>
                </el-table-column>
              </el-table><br/><br/>
              <el-tag size="small" >已诊患者：</el-tag>
              <el-table ref="singleTable" :data="RegistersTable02" highlight-current-row
                        @row-dblclick = "handleDBCurrentChange"
                        style="width: 100%;height: 50%" :show-header="false" size="small">
                <el-table-column property="casenumber" label="病历号" width="80">
                </el-table-column>
                <el-table-column property="realname" label="姓名" >
                </el-table-column>
                <el-table-column label="年龄" width="50">
                  <template #default="solt">
                    {{solt.row.age}}{{solt.row.agetype}}
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
            <el-tab-pane label="科室*" name="department">
              <el-tag>未诊患者：</el-tag>

              <el-table ref="singleTable" :data="MyPatientsData01" highlight-current-row
                        style="width: 100%;height: 50%" :show-header="false" size="small">
                <el-table-column property="casenumber" label="病历号" width="80">
                </el-table-column>
                <el-table-column property="realname" label="姓名" >
                </el-table-column>
                <el-table-column label="年龄" width="50">
                  <template #default="solt">
                    {{solt.row.age}}{{solt.row.agetype}}
                  </template>
                </el-table-column>
              </el-table>
              <el-tag>已诊患者：</el-tag>
              <el-table ref="singleTable" :data="MyPatientsData02"
                        style="width: 100%;height: 50%" :show-header="false" size="small">
                <el-table-column property="casenumber" label="病历号" width="80">
                </el-table-column>
                <el-table-column property="realname" label="姓名" >
                </el-table-column>
                <el-table-column label="年龄" width="50">
                  <template #default="solt">
                    {{solt.row.age}}{{solt.row.agetype}}
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </el-main>
      </el-container>
    </el-aside>

    <el-container direction="vertical" style="height: 950px">
      <el-row>
        <span style="margin-left: 20px;font-size: small;" :span="20">{{showPatientsTop}}</span>
        <el-button type="text" size="small" style="float: right;margin-right: 20px;" @click="endRegister" v-if="showStop" :span="2">诊毕</el-button>
      </el-row>

      <!--
        门诊主要功能区
      -->
      <el-tabs type="border-card" :before-leave="toTab" v-model="currentTab">
        <!-- 患者检查填写  -->
        <el-tab-pane label="病历首页" name="1">
          <!-- 1. 门诊病历 -->
          <el-container v-loading="loading">
            <!-- 页面正文 -->
            <el-main style="width:100%;background:#fff;height:800px;overflow-y: auto">
              <el-form ref="form" :model="medicalRecord01" label-width="80px" label-position="left" size="small" >
                <el-row style="background-color: #EAF1F5;margin-top: -20px">
                  <el-col :span="8" style="margin-top: 4px;"></el-col>
                  <el-col :span="4" >
                    <el-button type="text" size="small"  @click="stageCheck">暂存</el-button>
                  </el-col>
                  <el-col :span="4" >
                    <el-button type="text" size="small"  @click="saveCheck">提交</el-button>
                  </el-col>
                  <el-col :span="4" >
                    <el-button type="text" size="small"  :disabled="isSaved" @click="cleanCheck">清空所有</el-button>
                  </el-col>
                  <el-col :span="4" >
                    <el-button type="text" size="small"  @click="refMedicalRecord">刷新</el-button>
                  </el-col>
                </el-row>
                <el-tag>病史内容：</el-tag>
                <el-form-item label="主诉">
                  <el-input type="textarea" :rows="1" v-model="medicalRecord01.readme" :disabled="isSaved"></el-input>
                </el-form-item>
                <el-form-item label="现病史">
                  <el-input type="textarea" :rows="4" v-model="medicalRecord01.present" :disabled="isSaved"></el-input>
                </el-form-item>
                <el-form-item label="现病治疗情况">
                  <el-input type="textarea" :rows="4" v-model="medicalRecord01.presenttreat" :disabled="isSaved"></el-input>
                </el-form-item>
                <el-form-item label="既往史">
                  <el-input type="textarea" :rows="4" v-model="medicalRecord01.history" :disabled="isSaved"></el-input>
                </el-form-item>
                <el-form-item label="过敏史">
                  <el-input type="textarea" :rows="4" v-model="medicalRecord01.allergy" :disabled="isSaved"></el-input>
                </el-form-item>
                <el-form-item label="体格检查">
                  <el-input type="textarea" :rows="4" v-model="medicalRecord01.physique" :disabled="isSaved"></el-input>
                </el-form-item>
                <el-form-item label="诊断">
                  <el-input type="textarea" :rows="4" v-model="medicalRecord01.diagnosis" :disabled="isSaved"></el-input>
                </el-form-item>
                <el-form-item label="检查建议">
                  <el-input type="textarea" :rows="4" v-model="medicalRecord01.proposal" :disabled="isSaved"></el-input>
                </el-form-item>
                <el-form-item label="注意事项">
                  <el-input type="textarea" :rows="4" v-model="medicalRecord01.careful" :disabled="isSaved"></el-input>
                </el-form-item>
              </el-form>
              <el-button type="text" size="small"  @click="saveCheck">提交</el-button>
            </el-main>

          </el-container>
        </el-tab-pane>
        <!-- 医生给患者根据病历要求患者进行的检查项：一般无比对标准，如B超 -->
        <el-tab-pane label="检查申请" :lazy="true" name="2">

        </el-tab-pane>
        <!-- 医生给患者根据病历要求患者进行的检验项：一般有比对标准，如血常规 -->
        <el-tab-pane label="检验申请*" :lazy="true" name="3">
          <div  >
            <!-- 3.检验申请 -->
            <el-container direction="horizontal">
              <!-- 页面弹窗 -->
              <el-dialog title="新增检验申请" v-model="dialog01Visible" width="30%">
                <el-form size="small">
                  <el-form-item>
                    <el-input v-model="searchKeyWords" placeholder="输入检验项目名称" style="width: 100%" @input="handleSearch">
                      <template #prepend>检验名称：</template>
<!--                      <template slot="prepend">检验名称：</template>-->
<!--                      <el-button slot="append" icon="el-icon-search"></el-button>-->
                    </el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-table border :data="filteredItems" size="small" @selection-change="handleMultiSelection">
<!--                      <el-table-column  label="" width="30px">-->
<!--                        <template #default="{ row }">-->
<!--                          <input-->
<!--                              type="radio"-->
<!--                              name="chooseOne02"-->
<!--                              :checked="formData.selectedItem?.id === row.id"-->
<!--                              @change="formData.selectedItem = row"-->
<!--                          />-->
<!--                        </template>-->
<!--                      </el-table-column>-->
                      <el-table-column type="selection" width="55"></el-table-column>
                      <el-table-column property="recordtype" label="分类" width="80">
                      </el-table-column>
                      <el-table-column property="itemname" label="项目" >
                      </el-table-column>
                    </el-table>
                  </el-form-item>
                  <el-form-item label="检验科室" >
                    <el-select  placeholder="请选择" v-model="formData.deptname">
                      <el-option
                          v-for="item in selectData01"
                          :key="item.id"
                          :label="item.deptname"
                          :value="item.deptname">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="是否加急：" >
                    <el-switch
                        v-model="formData.isurgent"
                        active-value="1"
                        inactive-value="0"
                        active-color="#13ce66">
                    </el-switch>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="addApply">确 定</el-button>
                    <el-button @click="dialog01Visible = false">取 消</el-button>
                  </el-form-item>
                </el-form>
              </el-dialog>
              <!-- 页面正文 -->
              <el-aside  style="width: 900px;height: 500px;">

                <el-row style="background-color: #EAF1F5">
                  <el-col :span="8" style="margin-top: 4px;">
                    <el-tag size="small">本项目金额合计：</el-tag>
                    <el-tag type="warning" size="small">{{totalAmount}}</el-tag>
                  </el-col>
                  <el-col :span="3"><el-button type="text" size="small"  @click="newApply">新增项目</el-button></el-col>
                  <el-col :span="3"><el-button type="text" size="small"  @click="deleteApply">删除项目</el-button></el-col>
                  <el-col :span="2"><el-button type="text" size="small"  @click="saveApply">开立</el-button></el-col>
                </el-row>

                <el-form ref="form"  label-width="100px">
                  <el-row>
                    <el-col :span="2" style="margin-top: 40px;">
                      <el-input type="textarea" :rows="4" :disabled="true" style="width: 45px;" v-model="msg01">
                      </el-input>
                    </el-col>
                    <el-col :span="22" style="margin-top: 4px;">
                      <el-table ref="singleTable" :data="tableData02"
                                style="width: 100%;margin: 0px;padding: 0px;" :show-header="true">
                        <el-table-column  type="selection">
                        </el-table-column>
                        <el-table-column label="分类" width="80">
                          <template #default="solt">
                            {{ categoryMap[solt.row.itemid]  }}
                          </template>
                        </el-table-column>
                        <el-table-column prop="name" label="项目" >
                        </el-table-column>
                        <el-table-column prop="deptname" label="执行科室" width="80">
                        </el-table-column>
                        <el-table-column  label="加急" width="70">
                          <template #default="solt">
                            <span v-if="solt.row.isurgent==1" >是</span>
                            <sapn v-if="solt.row.isurgent==0" >否</sapn>
                          </template>
                        </el-table-column>
                        <el-table-column  label="状态" width="80">
                          <template #default="solt">
                            <span v-if="solt.row.state==0" >未开立</span>
                            <span v-if="solt.row.state==1" >已开立</span>
                            <sapn v-if="solt.row.state==2" >已收费</sapn>
                            <sapn v-if="solt.row.state==3" >已执行</sapn>
                            <sapn v-if="solt.row.state==4" >已完成</sapn>
                            <sapn v-if="solt.row.state==5" >已退费</sapn>
                          </template>
                        </el-table-column>
                        <el-table-column  label="单价" width="80">
                          <template #default="solt">
                            {{ priceMap[solt.row.itemid]  }}
                          </template>
                        </el-table-column>
                        <el-table-column  label="检验结果" width="80">
                          <template #default="solt">
                              <el-button link type="primary" size="small" @click="checkResult(solt)">查看</el-button>
                          </template>
                        </el-table-column>
                      </el-table>
                    </el-col>
                  </el-row>

                  <el-row>
                    <el-form-item label="目的要求">
                      <el-input type="textarea" :rows="4" placeholder="【检验目的：】
【患者主诉：】
【现病史：】" v-model="objective"></el-input>
                    </el-form-item>
                  </el-row>

                </el-form>

                <el-dialog title="检验结果" v-model="dialog02Visible" width="30%">
                  <el-form size="small">
                    <el-form-item>
                      <el-input v-model="singleResult" style="width: 100%"  :readonly="true" placeholder="暂无检验结果">
                      </el-input>
                    </el-form-item>
                  </el-form>
                </el-dialog>

              </el-aside>
            </el-container>

          </div>
        </el-tab-pane>
        <!-- 患者病历填写  -->
        <el-tab-pane label="门诊确诊*" :lazy="true" name="4">
          <div  >
            <el-container direction="horizontal">
              <!-- 页面正文 -->
              <el-aside  style="width: 900px;height: 500px;">

                <el-row style="background-color: #EAF1F5">
                  <el-col :span="20"><el-button type="text" size="small"  @click="over">确诊</el-button></el-col>
                </el-row>

                <el-form ref="form"  label-width="100px">
                  <el-row>
                    <el-col :span="2" style="margin-top: 40px;">
                      <el-input type="textarea" :rows="4" :disabled="true" style="width: 45px;" v-model="msg01">
                      </el-input>
                    </el-col>
                    <el-col :span="22" style="margin-top: 4px;">
                      <el-table ref="singleTable" :data="tableData02"
                                style="width: 100%;margin: 0px;padding: 0px;" :show-header="true">
                        <el-table-column label="分类" width="80">
                          <template #default="solt">
                            {{ categoryMap[solt.row.itemid]  }}
                          </template>
                        </el-table-column>
                        <el-table-column prop="name" label="项目" >
                        </el-table-column>
                        <el-table-column prop="deptname" label="执行科室" width="80">
                        </el-table-column>
                        <el-table-column  label="加急" width="70">
                          <template #default="solt">
                            <span v-if="solt.row.isurgent==1" >是</span>
                            <sapn v-if="solt.row.isurgent==0" >否</sapn>
                          </template>
                        </el-table-column>
                        <el-table-column  label="状态" width="80">
                          <template #default="solt">
                            <span v-if="solt.row.state==0" >未开立</span>
                            <span v-if="solt.row.state==1" >已开立</span>
                            <sapn v-if="solt.row.state==2" >已收费</sapn>
                            <sapn v-if="solt.row.state==3" >已执行</sapn>
                            <sapn v-if="solt.row.state==4" >已完成</sapn>
                            <sapn v-if="solt.row.state==5" >已退费</sapn>
                          </template>
                        </el-table-column>
                        <el-table-column  label="单价" width="80">
                          <template #default="solt">
                            {{ priceMap[solt.row.itemid]  }}
                          </template>
                        </el-table-column>
                        <el-table-column  label="检验结果" width="80">
                          <template #default="solt">
                            <el-button link type="primary" size="small" @click="checkResult(solt)">查看</el-button>
                          </template>
                        </el-table-column>
                      </el-table>
                    </el-col>
                  </el-row>

                  <el-row>
                    <el-form-item label="进行确诊">
                      <el-input type="textarea" :rows="4" placeholder="【进行确诊：】" v-model="result"></el-input>
                    </el-form-item>
                  </el-row>

                </el-form>

                <el-dialog title="检验结果" v-model="dialog02Visible" width="30%">
                  <el-form size="small">
                    <el-form-item>
                      <el-input v-model="singleResult" style="width: 100%"  :readonly="true" placeholder="暂无检验结果">
                      </el-input>
                    </el-form-item>
                  </el-form>
                </el-dialog>

              </el-aside>
            </el-container>

          </div>
        </el-tab-pane>
      </el-tabs>
    </el-container>
  </el-container>
</template>


<script setup>
import {onMounted, ref,computed} from 'vue'
import {ElMessage, ElMessageBox, ElNotification} from 'element-plus'
import service from '@/utils/request'
import {Search} from "@element-plus/icons-vue";
import {useCounterStore}  from "@/stores/counter.js";
// 引入pinia
let counter = useCounterStore()
// 引入本人未诊
let RegistersTable01 = ref()
// 引入本人已诊
let RegistersTable02 = ref()
// 引入科室未诊
let MyPatientsData01 = ref()
// 引入科室已诊
let MyPatientsData02 = ref()
// 本人&科室切换
let activeTab = ref('personal')
// 右侧切换
let currentTab = ref('1')
// 加载条件构造
// let register = ref({
//   id:'',
//   casenumber:'',
//   realname: '',
//   gender: '',
//   idnumber: '',
//   birthdate: '',
//   age: '',
//   agetype: '',
//   homeaddress: '',
//   visitdate: '',
//   noon: '',
//   deptid: '',
//   userid: '',
//   registleid: '',
//   settleid: '',
//   isbook: '',
//   registtime: '',
//   registerid: '',
//   visitstate: '',
// })
let medicalRecord = ref({MedicalDisease01:[],MedicalDisease02:[]})
//病历首页
let medicalRecord01 = ref({})
let showPatientsTop = ref("请选择患者")
let casenumber = ref('')
let realname = ref('')
let registid = ref('')
let inputPatient = ref('')
//检验申请
let msg01 = ref('检验项目')
//检验申请信息
let tableData02 = ref([])
//价格映射信息
let priceMap = ref([])
//分类映射表信息
let categoryMap = ref([])
// 添加项目对话框可见
let dialog01Visible = ref(false)
// 检查结果对话框可见
let dialog02Visible = ref(false)
// 检查结果
let singleResult = ref('')
// 筛选项目数据
let filteredItems = ref()
// 执行科室数据
let selectData01 = ref()
// 查询项目关键词
let searchKeyWords = ref('')
// 多选相关
const selectedItems = ref([])
const handleMultiSelection = (rows) => {
  selectedItems.value = rows
}                 // 存储选中的多个项目
// 添加项目数据
let formData = ref({
  isurgent: 0,
})
// 检验项目表格引用
let singleTable = ref()
//目的要求
let objective = ref('')
//刷新患者列表
function reloadUser(){
  listData(null)
  inputPatient.value = ''
  casenumber.value = ''
  realname.value = ''
  registid.value = ''
  showPatientsTop.value = '请选择患者'
  medicalRecord01.value = ''
  tableData02.value = ''
  activeTab.value = 'personal'
  currentTab.value = '1'
  ElMessage.success('刷新成功')
}
// 查询患者姓名模糊查询
function checkwithname(){
  listData(inputPatient.value)
}
//双击选中患者
function handleDBCurrentChange(row){   //双击选中患者
  casenumber.value = row.casenumber
  realname.value = row.realname
  registid.value = row.id
  showPatientsTop.value = "病历号:" + casenumber.value + "  姓名:" + realname.value
  medicalRecord01.value = {}
  tableData02.value = []
  const registerid = row.id
  service.get("/medicalrecord/getbyregisterid?registerid="+registerid).then(res =>{
    if(res.data){
      medicalRecord01.value = res.data
      console.log(medicalRecord01.value)
    }
  })
  service.get("/checkapply/getbyregistid?registid="+registerid).then(res =>{
    if(res.data){
      tableData02.value = res.data
      console.log(tableData02.value)
    }
  })
}
//刷新病例页
function refMedicalRecord(){
  casenumber.value = casenumber.value
  realname.value = realname.value
  registid.value = registid.id
  showPatientsTop.value = "病历号" + casenumber.value + "  姓名" + realname.value
  const registerid = medicalRecord01.value.registid
  service.get("/medicalrecord/getbyregisterid?registerid="+registerid).then(res =>{
    medicalRecord01.value = res.data
    console.log(medicalRecord01.value)
        if(res.data){
          ElNotification({
            title: '提示',
            message: '刷新成功!!!!!!!!!!!!!',
            type: 'success',
          })
        }else{
          ElNotification({
            title: '提示',
            message: '刷新失败!!!!!!!!!!!!!',
            type: 'warning',
          })
        }
  }
  )
}
//清空病例详情
function cleanCheck(){
  medicalRecord01.value = {}
  casenumber.value = ''
  realname.value = ''
  registid.value = ''
  tableData02.value = ''
  showPatientsTop.value = "请选择患者"
}
//暂存
function stageCheck(){
  medicalRecord01.value.casenumber = casenumber.value
  medicalRecord01.value.casestate = 1
  medicalRecord01.value.registid = registid.value
  service.post("/medicalrecord/saveOrUpdate", medicalRecord01.value).then(({status,data}) =>{
    if ( data){
      const visitstate = 2
      service.put("/register/updatestate?registid="+registid.value+"&visitstate="+visitstate).then(res=>{
        if (res.data){
          listData(null)
        }
      })
      ElNotification({
        title: '提示',
        message: '暂存成功!!!!!!!!!!!!!',
        type: 'success',
      })
      medicalRecord01.value = ''
      tableData02.value = ''
      casenumber.value = ''
      realname.value = ''
      registid.value = ''
      showPatientsTop.value = "请选择患者"
    }else{
      ElNotification({
        title: '提示',
        message: '暂存失败!!!!!!!!!!!!!',
        type: 'warning',
      })
    }
  })
}
//提交
function saveCheck(){
  medicalRecord01.value.casenumber = casenumber.value
  medicalRecord01.value.casestate = 2
  medicalRecord01.value.registid = registid.value
  service.post("/medicalrecord/saveOrUpdate", medicalRecord01.value).then(({status,data}) =>{
    if (data){
      const visitstate = 2
      service.put("/register/updatestate?registid="+registid.value+"&visitstate="+visitstate).then(res=>{
        if (res.data){
          listData(null)
        }
      })
      ElNotification({
        title: '提示',
        message: '提交成功!!!!!!!!!!!!!',
        type: 'success',
      })
      medicalRecord01.value = ''
      tableData02.value = ''
      casenumber.value = ''
      realname.value = ''
      registid.value = ''
      showPatientsTop.value = "请选择患者"
    }else{
      ElNotification({
        title: '提示',
        message: '提交失败!!!!!!!!!!!!!',
        type: 'warning',
      })
    }
  })
}
//获取项目单价和分类
function getPrice(){
  service.get("/fmeditem/list").then(res=>{
    res.data.forEach((item) => {
      priceMap.value[item.id] = item.price; // 建立映射
      categoryMap.value[item.id] = item.recordtype;
    });
  })
}
//检查结果
function checkResult(solt){
  dialog02Visible.value = true
  singleResult.value = solt.row.result
}
//计算总金额
let totalAmount = computed(() => {
  if (!tableData02.value || tableData02.value.length === 0) {
    return '0元'
  }
  // 计算总金额（假设 priceMap 存储了每个项目的价格）
  const total = tableData02.value.reduce((sum, item) => {
    const price = priceMap.value[item.itemid] || 0
    return sum + price
  }, 0)
  return `${total}元`
})
//项目模糊查询
function handleSearch(){
  service.get('/fmeditem/getbyname?itemname='+searchKeyWords.value).then( res =>{
    filteredItems.value = res.data
  })
}
//新增项目
function newApply(){
  searchKeyWords.value = ''
  formData.value = {
    isurgent: 0,
  }
  filteredItems.value = []
  selectedItems.value = []
  dialog01Visible.value=true
}
//添加项目
async function addApply(){
  //数据检验
  if (selectedItems.value.length === 0) {
    ElNotification.warning('请至少选择一个项目')
    return
  }
  if (!formData.value.deptname) {
    ElNotification.warning('请选择检验科室')
    return
  }
  //首先整合数据
  const medicalrecord = (await service.get('/medicalrecord/getbyregisterid?registerid=' + registid.value)).data
  const newItems = selectedItems.value.map(item => ({
    medicalid: medicalrecord.id,
    registid: registid.value,
    itemid: item.id,
    name: item.itemname,
    objective: '',
    position: '',
    isurgent: formData.value.isurgent || 0,
    num:1,
    creationtime:'',
    doctorid: counter.vo_user.value.id,
    checkoperid: '',
    resultoperid: '',
    checktime: '',
    result: '',
    resulttime: '',
    state: 0,
    recordtype: 2,
    deptname: formData.value.deptname
  }))
  // 然后往tableData02里插入
  // 添加到本地表格
  if (!tableData02.value) {
    tableData02.value = []
  }
  tableData02.value.push(...newItems)
  ElNotification.success(`成功添加 ${newItems.length} 个项目`)
  dialog01Visible.value = false
}
//删除项目
function deleteApply(){
  if (!tableData02.value || tableData02.value.length === 0) {
    ElNotification.warning('没有可删除的项目')
    return
  }
  // 获取选中的行（Element Table 会通过 selection-change 事件更新 ref）
  const selectedRows = singleTable.value.getSelectionRows()

  if (selectedRows.length === 0) {
    ElNotification.warning('请先选择要删除的项目')
    return
  }

  // 过滤出未开立的项目（state=0）
  const itemsToDelete = selectedRows.filter(row => row.state == 0)

  if (itemsToDelete.length === 0) {
    ElNotification.warning('只能删除未开立的项目!')
    return
  }

  // 从 tableData02 中移除选中的未开立项目
  tableData02.value = tableData02.value.filter(
      item => !itemsToDelete.some(selected => selected.itemid === item.itemid  && selected.state === item.state)
  )

  ElNotification.success(`已删除 ${itemsToDelete.length} 个项目`)
}
//开立项目
function saveApply(){
  //获取选择的未开立的项目
  const selectedItems = singleTable.value.getSelectionRows().filter(item => item.state === 0)
  if (selectedItems.length === 0) {
    ElNotification.warning('请先选择未开立的项目')
    return
  }
  //整理批量提交的数据
  const submitData = selectedItems.map(item => ({
    ...item,
    state: 1, // 更新状态为"已开立"
    objective: objective.value,
  }))
  //调用后端插入数据
  service.post("/checkapply/batchInsert",submitData).then(({status,data})=>{
    if(data){
      ElNotification({
        title: '提示',
        message: '开立成功',
        type: 'success',
      })
      service.get("/checkapply/getbyregistid?registid="+registid.value).then(res =>{
        tableData02.value = res.data
        console.log(tableData02.value)
      })
      objective.value = ''
    }else{
      ElNotification({
        title: '提示',
        message: '开立失败',
        type: 'error',
      })
    }
  })
}
//获取执行科室
function getExecuteDept(){
  service.get("/dept/list?depttype=2").then(res =>{
    selectData01.value = res.data
  })
}
//确诊
let result = ref('')
function over(){
  ElMessageBox.confirm(`确定要确诊吗？`,
      '确诊确认',
      {
        confirmButtonText: '确认确诊',
        cancelButtonText: '取消',
        type: 'warning',
        lockScroll: false
      }
  ).then(() => {
    const visitstate = 3
    service.put("/register/updatestate?registid="+registid.value+"&visitstate="+visitstate+"&result="+result.value).then(res=>{
      if (res.data){
        service.put("/medicalrecord/over?registid="+registid.value+"&result="+result.value)
        listData(null)
        ElNotification({
          title: '提示',
          message: '确诊成功!!!!!!!!!!!!!',
          type: 'success',
        })
      }
    })
  })
}
//加载数据
function listData(a){
  // // 加载本人已诊患者，未诊患者，本科室已诊患者，未诊患者
  // register.value.userid=counter.vo_user.value.id,
  // register.value.visitstate=1
  // service.get("/register/conditionselect", register).then(res =>{
  //      RegistersTable01.value = res.data
  //      console.log(RegistersTable01.value)
  // })
  // register.value.userid=counter.vo_user.value.id,
  // register.value.visitstate=2
  // service.get("/register/conditionselect", register).then(res =>{
  //   RegistersTable02.value = res.data
  //   console.log(RegistersTable02.value)
  // })
  // register.value.deptid=counter.vo_user.value.deptid,
  // register.value.visitstate=1
  // service.get("/register/conditionselect", register).then(res =>{
  //   MyPatientsData01.value = res.data
  //   console.log(MyPatientsData01.value)
  // })
  // register.value.deptid=counter.vo_user.value.deptid,
  // register.value.visitstate=2
  // service.get("/register/conditionselect", register).then(res =>{
  //   MyPatientsData02.value = res.data
  //   console.log(MyPatientsData02.value)
  // })
  service.get("/register/conditionselect?userid="+counter.vo_user.value.id+"&visitstate=1"
      +"&realname="+a+"&deptid=0").then(res =>{
    RegistersTable01.value = res.data
    console.log(RegistersTable01.value)
  })
  service.get("/register/conditionselect?userid="+counter.vo_user.value.id+"&visitstate=2"
      +"&realname="+a+"&deptid=0").then(res =>{
    RegistersTable02.value = res.data
    console.log(RegistersTable02.value)
  })
  service.get("/register/conditionselect?deptid="+counter.vo_user.value.deptid+"&visitstate=1"
      +"&realname="+a+"&userid=0").then(res =>{
    MyPatientsData01.value = res.data
    console.log(MyPatientsData01.value)
  })
  service.get("/register/conditionselect?deptid="+counter.vo_user.value.deptid+"&visitstate=2"
      +"&realname="+a+"&userid=0").then(res =>{
    MyPatientsData02.value = res.data
    console.log(MyPatientsData02.value)
  })
}
onMounted(() => {
  listData(null)
  getPrice()
  getExecuteDept()
  console.log(priceMap.value)
})
</script>